<?php
//
//  EDIT
//
require_once('header.php');

if ( isset($_GET['search']) ) {
    // Accept get requests for searches. This has largely been deprecated, but remains for inacitve user searches.
    unset($_GET['user']);
    if ($_GET['search'] == 'inactive') {
        $found_users = User::find_by_unsubscribed();
        if ( !$found_users )  $session->add_message("Sorry, there are no unsubscribed users, perhaps you would like to create a new user?");
    }
}





if ( isset( $_GET['user'] ) && !empty( $_GET['user'] ) ) {
    $user = User::find_by_id( $_GET['user'] );
    $subs = Subscription::find_by_user_id($user->user_id);
} else {
    $user = false;
    $subs = false;
}

  /*
   * 
   *  EDIT.PHP
   * 
   *   THIS TEMPLATE IS RESPONSIBLE FOR SELECTING THE CORRECT TEMPLATES TO DISPLAY FOR THE CRUD OF
   *    USERS
   *    SUBSCRIPTIONS
   *    ALLOWANCES
   *    
   *    THE FLAG RESONSIBLE FOR THIS IS $template_type.
   * 
   * 
   */      
         view($subs);
?>
    
    
 
    <section>
        <header>
            <?php include_once('nav.edit.php'); ?>
        </header>
        
        <div class="table-frame padding">
            
        <?php
        $has_sub = ( $user ) ? Subscription::has_subscription( $user->user_id ) : false;
        if( $user && $has_sub && $subs ) include('template.sub-details.php');
        ?>
                
 	     <table class="subscription-table">
	         <thead>
                <tr class="heading-row skin-gradient">
                    <th colspan="2">
                    <?php 
                    if ( $user ) {
                        echo 'EDITING: #' . $user->user_id.'';
                    } else {
                        echo 'CREATE NEW USER';
                    }
                    ?>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <?php 
                    //
                    //  Get the user form.
                    //
                    require_once('template.user-form.php');
                    
                    if ( !$user ) {
                        require_once('template.select-unsubscribed.php'); 
                    } else {
                        if ( $has_sub ) {
                            require_once('template.edit-sub-form.php');
                        } else {
                            require_once('template.new-sub-form.php');
                        }
                         
                    }
                    
                    ?>
                </tr>
            </tbody>
        </table>
            
    </div>
</section>

<?php
//
//  End page.
//
require_once('footer.php');
?>




